<?php
declare (strict_types = 1);

namespace app\admin\controller\message;

use app\admin\BaseController;
use app\common\validate\Message as MessageValidate;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Log;
use app\common\model\message\Message as MessageModel;

class Message extends BaseController
{
    //是否关联user模型( 用于权限规则验证 )
    public $relateUser = true;

    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['list','update','delete','batchDel']],
    ];

    //显示资源列表
    public function index()
    {
        return view('index');
    }

    //列表数据
    public function list(){
        try{
            //获取列表数据
            $data = MessageModel::getListData($this->ruleWhere());


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);

    }

    //显示指定的资源
    public function read(int $id)
    {
        $model = $this->getModel(MessageModel::class, $id,['user']);
        if ($model->visited == MessageModel::VISITED_NO){
            $model->save([
                'visited' => MessageModel::VISITED_YES
            ]);
        }
        return view('read',[
            'model' => $model
        ]);
    }

    //显示编辑资源表单页
    public function edit(int $id)
    {
        return view('edit',[
            'model' => $this->getModel(MessageModel::class, $id)
        ]);
    }

    //保存更新的资源
    public function update(int $id)
    {
        try{
            //验证数据
            $data = request()->only(['name','email','title','message']);

            validate(MessageValidate::class)->check($data);

            if( $this->getModel(MessageModel::class, $id)->save($data) === false)
                throw new Exception('修改留言失败.');

            return json([
                'code' => 0,
                'msg' => '修改留言成功.'
            ]);


        } catch (ValidateException $e) {

            //验证错误
            return json(['code'=>1,'msg'=>$e->getMessage()]);
        } catch (\Exception $e){

            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }
    }

    //删除指定资源
    public function delete(int $id)
    {
        try{
            //获取参数
            $model = $this->getModel(MessageModel::class, $id);

            if ($model->delete() === false)
                throw new Exception('删除失败.');

            return json([
                'code' => 0,
                'msg' => '删除成功.'
            ]);
        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }
    }

    //批量删除
    public function batchDel()
    {
        try{
            $ids = explode(',',trim(request()->param('ids'),','));

            if( $this->batchRule(MessageModel::class, (array) $ids) &&
                MessageModel::destroy($ids) === false )
            {
                throw new Exception('批量删除留言失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }

        return json([
            'code' => 0,
            'msg' => '批量删除留言成功.'
        ]);
    }
}
